//
//  QpCityDetailsViewController.m
//  Tour
//
//  Created by 才诗琦 on 16/3/10.
//  Copyright © 2016年 张中烨. All rights reserved.
//

#import "QpCityDetailsViewController.h"
#import "PrefixHeader.pch"
@interface QpCityDetailsViewController ()<UITableViewDelegate, UITableViewDataSource>

@property(nonatomic, retain)NSMutableArray *dataArr;/**<tableView数组*/
@property(nonatomic, retain)NSMutableArray *photos; /**<轮播图数组*/
@property(nonatomic, retain)UITableView *tableView;
@property(nonatomic, retain)UIView *headerView; /**<添加在tableHeaderView上,轮播图添加在此view上*/
@property(nonatomic, strong)NSMutableArray *weatherArr;/**<天气数组*/
@property(nonatomic, strong)UILabel *weatherLabel;/**<气温label*/

@property(nonatomic, strong)UIImageView *ima;

@end

@implementation QpCityDetailsViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithImage:[[UIImage imageNamed:@"返回"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal] style:UIBarButtonItemStylePlain target:self action:@selector(back)];
    

    /**<数据处理*/
    [self dataHandler];
    
    /**<创建tableView*/
    [self creatTableView];
    
   self.ima = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 240)];
    self.ima.image = [UIImage imageNamed:@"14"];
    //开启用户交互
    self.ima.userInteractionEnabled = YES;
    
    //城市
    UILabel *cityLabel = [[UILabel alloc] initWithFrame:CGRectMake(SCREEN_WIDTH - 50, 10, 30, 100)];
    cityLabel.numberOfLines = 0;
    cityLabel.font = [UIFont systemFontOfSize:30];

    [self.ima addSubview:cityLabel];
    //天气(当天)

  
   
    UILabel *weekLabelD = [[UILabel alloc] initWithFrame:CGRectMake(50, 10, 80, 80)];
    
    weekLabelD.font = [UIFont systemFontOfSize:24];

    [self.ima addSubview:weekLabelD];
    
    
    
    UILabel *weatherLabel = [[UILabel alloc] initWithFrame:CGRectMake(SCREEN_WIDTH - 180, 40, 50, 40)];
    weatherLabel.font = [UIFont systemFontOfSize:20];
    [self.ima addSubview:weatherLabel];
    
    UILabel *timelabel = [[UILabel alloc] initWithFrame:CGRectMake(SCREEN_WIDTH - 180 , 90, 100, 30)];

    [self.ima addSubview:timelabel];
    //前一天
    
    UILabel *weekLabel = [[UILabel alloc] initWithFrame:CGRectMake(70, 130, 100, 30)];

    [self.ima addSubview:weekLabel];
    
    UILabel *timelabelY = [[UILabel alloc] initWithFrame:CGRectMake(70, 170, 100, 30)];

    [self.ima addSubview: timelabelY];
    
    
    UILabel *weatherY = [[UILabel alloc] initWithFrame:CGRectMake(10, 130, 50, 50)];
 weatherY.font = [UIFont systemFontOfSize:20];
    [self.ima addSubview:weatherY];
    
    //第二天
    
    UILabel *weekLabel2 = [[UILabel alloc] initWithFrame:CGRectMake(260, 130, 100, 30)];

    [self.ima addSubview:weekLabel2];
    
    UILabel *timelabelY2 = [[UILabel alloc] initWithFrame:CGRectMake(260, 170, 100, 30)];
//    timelabelY2.backgroundColor = [UIColor yellowColor];
    [self.ima addSubview: timelabelY2];
    
    
    UILabel *weatherY2 = [[UILabel alloc] initWithFrame:CGRectMake(200, 130, 50, 50)];
 weatherY2.font = [UIFont systemFontOfSize:20];
    [self.ima addSubview:weatherY2];
    
    
       _tableView.tableHeaderView = self.ima;
    
    NSString *url = [NSString stringWithFormat:@"http://app.tianqi.com/index.php?a=json15&area=%@&c=app&city=%@&showid=3&token=&type=1",self.str,self.str];
    [AFNModel GETWithHandler:url completion:^(id result) {
        NSMutableArray *arr = [NSMutableArray array];
        
        arr = result[@"resultvalue"];
        

            cityLabel.text = self.str;
        weekLabelD.text = arr[1][@"week"];
        weatherLabel.text = arr[1][@"weanight"];
        timelabel.text = [NSString stringWithFormat:@"%@ ~ %@",arr[1][@"ltemp"],arr[1][@"htemp"]];
        
        
        weekLabel.text = arr[0][@"week"];
        weatherY.text = arr[0][@"weanight"];
        timelabelY.text =[NSString stringWithFormat:@"%@ ~ %@",arr[0][@"ltemp"],arr[0][@"htemp"]];
        
        weekLabel2.text = arr[2][@"week"];
        weatherY2.text = arr[2][@"weanight"];
        timelabelY2.text =[NSString stringWithFormat:@"%@ ~ %@",arr[2][@"ltemp"],arr[2][@"htemp"]];
        

        
        
    }];
    
    
}
- (void)back
{
    
    [self.navigationController popViewControllerAnimated:YES];
}

#pragma mark - 创建tableView
- (void)dataHandler
{
    // 通过传过来的热门国家id拼接网址
    NSString *url = [NSString stringWithFormat:@"http://open.qyer.com/qyer/footprint/city_detail?city_id=%@",self.cityid];
    NSLog(@"_cityid:%@",_cityid);
    NSString *str = @"&client_id=qyer_ios&client_secret=cd254439208ab658ddf9&count=20&oauth_token=89dd34fbffaaa72503b4baa7a9d02812&page=1&track_app_channel=App%2520Store&track_app_version=6.8.3&track_device_info=iPhone%25204%28ChinaMobile%2CChinaUnicom%29&track_deviceid=CDA3EEEB-61BF-473E-AFFF-32B6332C9BBF&track_os=ios%25207.1.2&track_user_id=7328550&v=1";
    NSString *newStr = [NSString stringWithFormat:@"%@%@",url,str];
    
    [AFNModel GETWithHandler:newStr completion:^(id result) {
       
        
   
        /**< tableview数据获取 */
        // 转换数组类型
        NSMutableArray *arr = [result[@"data"][@"local_discount"] mutableCopy];
        // 合并数组
        [arr addObjectsFromArray:result[@"data"][@"new_discount"]];
        // 获取数据
        self.dataArr = [QpCityDetailModel modelHanderWithArray:arr];
        
        // 精彩当地游
        UILabel *celltitle = [[UILabel alloc]initWithFrame:CGRectMake(10, 210, 100, 25)];
        celltitle.text = @"精彩当地游";
        [_tableView.tableHeaderView addSubview:celltitle];
        // 查看更多
        UIButton *lookMore = [UIButton buttonWithType:UIButtonTypeSystem];
        lookMore.frame = CGRectMake(SCREEN_WIDTH - 100, 210, 90, 25);
        [lookMore setTitle:@"查看全部>" forState:UIControlStateNormal];
        [lookMore addTarget:self action:@selector(btn) forControlEvents:UIControlEventTouchUpInside];
        [self.ima addSubview:lookMore];

        // 刷新
        [_tableView reloadData];

        
    }];
}

#pragma mark - 查看更多 btn 触发方法
- (void)btn
{
    // 跳转界面隐藏tabbar
    self.hidesBottomBarWhenPushed=YES;
    QpLocalTravelViewController *qp = [[QpLocalTravelViewController alloc]init];
    qp.idstr = self.cityid;
    [self.navigationController pushViewController:qp animated:YES];
    

    NSLog(@"下级界面");
}

#pragma mark - 创建tabelView
- (void)creatTableView
{
    // tableView 初始化
    self.tableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT - 64) style:UITableViewStylePlain];
    // 设置代理人
    _tableView.delegate = self;
    _tableView.dataSource = self;
    // 隐藏滚动条
    _tableView.showsVerticalScrollIndicator = NO;
    _tableView.backgroundColor = [UIColor whiteColor];
//    [_tableView NightWithType:UIViewColorTypeNormal];
    //  注册重用池
    [_tableView registerClass:[QpCityDeatilsTableViewCell class] forCellReuseIdentifier:@"cell"];
    [self.view addSubview:_tableView];
   
}
#pragma mark - 必须实现的两个协议方法
// 分区中cell 个数
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return _dataArr.count;
}
// 设置cell内容
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    QpCityDeatilsTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell" forIndexPath:indexPath];
    QpCityDetailModel *qp = _dataArr[indexPath.row];
    

    // 图片
    [cell.photo sd_setImageWithURL:[NSURL URLWithString:qp.photo]placeholderImage:[UIImage imageNamed:@"zhanweitu.jpg"]];
    // 标题
    cell.title.text = qp.title;
//    [cell.title NightWithType:UIViewColorTypeNormal];
    // 价格
    cell.price.text = [[qp.price stringByReplacingOccurrencesOfString:@"<em>" withString:@""] stringByReplacingOccurrencesOfString:@"</em>" withString:@""];
//    [cell.price NightWithType:UIViewColorTypeNormal];
    // 折扣
    
    cell.priceoff.text = qp.priceoff;
    
//    [cell.priceoff NightWithType:UIViewColorTypeNormal];
    
//    [cell NightWithType:UIViewColorTypeNormal];
    return cell;
    
    
}
#pragma mark - 点击cell
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    //取消tableview 选中状态
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    // 跳转webView
    WebViewController *wv = [[WebViewController alloc]init];
   QpCityDetailModel *q = _dataArr[indexPath.row];
    wv.urlstr = [NSString stringWithFormat:@"http://m.qyer.com/z/deal/%@/?source=app&client_id=qyer_ios&track_app_version=6.8.3&track_deviceid=CDA3EEEB-61BF-473E-AFFF-32B6332C9BBF&track_user_id=7328550",q.cellid];
    wv.str = self.str;
    NSLog(@"cellid:%@",q.cellid);
   
    [self.navigationController pushViewController:wv animated:YES];

}
#pragma mark - 设置行高
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    
    return 100 * 200 / 300 + 20;
}



- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
